package ua.kpi.ipsa.expert.api.client;

import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

import net.miginfocom.swing.*;
import ua.kpi.ipsa.expert.api.net.*;

/**
 * 
 * @author doctorpro
 * 
 */
public class AssignedAlgoBrowser extends JPanel implements ActionListener {

	//
	private JFrame parent;

	//
	private JFrame frame;

	//
	private ClientFrameManager cfm;
	private JTable table;

	//
	private Vector<String> colNames;

	//
	private int portionId;
	
	//
	private JButton btnWatch;

	/**
	 * Create the panel.
	 */
	public AssignedAlgoBrowser(ClientFrameManager cfm, JFrame parent,
			int portionId) {
		this.cfm = cfm;
		this.parent = parent;
		this.portionId = portionId;

		colNames = new Vector<String>();
		colNames.add("Name");
		colNames.add("% complete");
		colNames.add("Gippos found");
		frame = cfm.createFrame();

		frame.setContentPane(this);
		setLayout(new MigLayout("", "[grow]", "[][grow][][]"));

		JButton btnAddAlgo = new JButton("Add");
		btnAddAlgo.addActionListener(new AddHandler());
		add(btnAddAlgo, "cell 0 0");

		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, "cell 0 1,grow");

		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getSelectionModel().addListSelectionListener(new ModelSelectionHandler());
		scrollPane.setViewportView(table);

		btnWatch = new JButton("Watch");
		btnWatch.setEnabled(false);
		btnWatch.addActionListener(new WatchHandler());
		add(btnWatch, "cell 0 2");

		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		add(panel, "cell 0 3,grow");

		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new BackHandler());
		panel.add(btnBack);

		//
		reloadAlgoTable();

		frame.setTitle("Algorithms");
		frame.pack();
		frame.setLocationByPlatform(true);
		frame.addWindowListener(new CloseHandler());
		frame.setVisible(true);
	}

	/**
	 * 
	 */
	private void onBack() {
		cfm.disposeFrame(frame);
		parent.setVisible(true);
	}

	/**
	 * 
	 * @return
	 */
	public void reloadAlgoTable() {

		//
		UsersConsole c = cfm.getConsole();

		try {
			java.util.List<CPAlgoSummaryData> l = c
					.getCPAlgoSummaryData(portionId);

			DefaultTableModel result = new IneditableDefaultTableModel(
					colNames, l.size());

			int count = 0;
			for (Iterator<CPAlgoSummaryData> i = l.iterator(); i.hasNext();) {
				CPAlgoSummaryData data = i.next();
				result.setValueAt(data.name, count, 0);
				result.setValueAt(String.format("%.1f%%", 100 * data.percentage), count, 1);
				result.setValueAt(data.hypoFound ? "DETECTED" : "-", count, 2);
				count++;
			}

			table.setModel(result);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @return
	 */
	private int getSelectedAlgoId() {
		
		return -1;
	}
	
	/**
	 * 
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		
		System.out.println("reloading algo table");
		reloadAlgoTable();
	}
	
	/**
	 * 
	 * @author doctorpro
	 *
	 */
	private class ModelSelectionHandler implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			btnWatch.setEnabled(true);
		}
	}

	/**
	 * 
	 * @author doctorpro
	 * 
	 */
	private class IneditableDefaultTableModel extends DefaultTableModel {

		public IneditableDefaultTableModel(Vector<String> cols, int size) {
			super(cols, size);
		}

		@Override
		public boolean isCellEditable(int r, int c) {
			return false;
		}
	}

	/**
	 * 
	 * @author doctorpro
	 * 
	 */
	private class WatchHandler implements ActionListener {

		/**
		 * 
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			frame.setVisible(false);
			new Hypo(cfm, frame, portionId, getSelectedAlgoId());
		}
	}

	/**
	 * 
	 * @author doctorpro
	 * 
	 */
	private class AddHandler implements ActionListener {

		/**
		 * 
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			frame.setVisible(false);
			new AddAlgo(cfm, frame, portionId, AssignedAlgoBrowser.this);
		}
	}

	/**
	 * 
	 * @author doctorpro
	 * 
	 */
	private class BackHandler implements ActionListener {

		/**
		 * 
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			onBack();
		}
	}

	/**
	 * 
	 * @author doctorpro
	 * 
	 */
	private class CloseHandler extends WindowAdapter {

		@Override
		public void windowClosing(WindowEvent e) {
			onBack();
		}
	}
}
